﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;

namespace SPWebShell.cmdlet
{
    /// <summary>
    /// A base class for all cmdlets.
    /// Rationale - simplified logging stubs
    /// </summary>
    public class CmdLetBase : PSCmdlet
    {
        ISPRepository _repos;

        public CmdLetBase()
        {
            _repos = new SPRepositoryImplementation(this.VerboseLogger, this.ErrorLogger);

        }
        /// <summary>
        /// Rational for having a wrapper function - usefule for unit testing. Stubs can be passed because WriteVerbose might not be available
        /// </summary>
        /// <param name="message"></param>
        protected void VerboseLogger(string message)
        {
            this.WriteVerbose(message);
        }
        protected void ErrorLogger(string message)
        {
            Exception ex = new Exception(message);
            WriteError(new ErrorRecord(ex, "", ErrorCategory.InvalidResult, null));
        }
        public ISPRepository SPRepos
        {
            get
            {
                return _repos;
            }
            set
            {
                _repos = value;
            }
        }
        /// <summary>
        /// Returns the current folder where the cmdlet is being executed from
        /// </summary>
        /// <returns></returns>
        public string GetCurrentDir()
        {
            return this.SessionState.Path.CurrentLocation.ToString();
        }
        /// <summary>
        /// Builds a full path to a file given a fragment. e.g. .\test.txt would resolve to c:\work\test.txt
        /// </summary>
        /// <param name="fileNameFragment"></param>
        /// <returns></returns>
        public string GetFullPathToFile(string fileNameFragment)
        {
            string curdir = this.GetCurrentDir();
            string path = System.IO.Path.Combine(curdir, fileNameFragment);
            return System.IO.Path.GetFullPath(path);
        }


    }
}
